/*
 * ================================================================================变量定义区
 */
var showCount = 50; //定义每页显示条数
var currentPage = 1;//全局变量，当前页
var survey_id = "";//问卷ID
/*
 * ================================================================================页面初始化函数
 */
$(function()
{
	//调查表ID赋值
	survey_id = $("#SurveyId").val();

	//初始化列表，取第一页数据
	getDataList(currentPage);
	
	//绑定导出按钮
	$("#QualityExportFunc").on("click", function()
	{
		$("#QualityExportForm").submit();
	});

});
/*
 * ================================================================================函数定义区
 * 
 */ 

/**
 * 获取问题信息
 * 
 * @param survey_Id
 */
function getQuestionContent(survey_Id) 
{
	var questions = null;
	$.ajax(
	{
		type : 'POST',
		url : BASE_PATH + '/survey/surveyStatistics/qualitySurveyStatistics/getQuestionContent.do',
		dataType : "json",
		data : 
		{
			"survey_Id" : survey_Id
		},
		cache : false,
		async: false,
		success : function(data) 
		{
			var html = "";
			questions = data.questions;
			if (questions != null) 
			{
				//  id="tr_index_'+ i + '"
				html += '<tr onclick="chooseRow(this,false);">' 
				     + '<th class="col-th-username">用户姓名</th>'
				     +'<th class="col-th-username">中文名称</th>'
				     +'<th class="col-th-username">部门名称</th>'
					 + '<th class="col-vote-date">投票时间</th>';
				
				for (var i = 0; i < questions.length; i++) 
				{
					html += '<th class="col-th-question-answer" title='
						 + switcHtml(questions[i].question_content) + '>'
						 + switcHtml(questions[i].question_content) + '</th>';
				}
				
				html += '<th style="width: 1px;"></th>' + '</tr>';
			}
			$('#QualityResult').html(html);
		},
		error : function(e) 
		{
			openAlert("加载数据错误！");
			return;
		}
	});
	return questions;
}

/**
 * 获取列表数据
 * @param pageIndex 请求页码
 */
function getDataList(pageIndex)
{
	// 获取问题信息
	var questions = getQuestionContent(survey_id);
	
	$.ajax(
	{
		type: 'post',
        url: BASE_PATH +'/survey/surveyStatistics/qualitySurveyStatistics/getStatisticsPageList.do',
        dataType: "json",
        data: 
        {
        	"showCount": showCount,
        	"currentPage": pageIndex,
        	"survey_id": survey_id,
			"query_condition_field_list":"survey_id"
        },
		cache : false,
		success : function(data, textStatus) 
        {
        	var 
        		//列表内容
        		tableContent = "",
        		//总记录条数
        		totalResult ="";

        	//统计结果列表填充
        	if(data.queryResult != null)
        	{
        		$.each(data.queryResult, function(index, result)
        		{
        			//投票时间
        			var time = getRealNull(result.last_Modify_Time);
        			//投票结果
        			var answers = '';
        			//遍历问题，对应各问题结果
        			$.each(questions, function(i, question)
        			{
        				if(result.answers)
        				{
        					var flag = false;
            				$.each(result.answers, function(j, answer)
            				{
            					if(question.id === answer.question_Id)
            					{
            						flag = true;
            						if(answer.answer=="1"){
            							answers += '<td class="col-td-question-answer" title='+switcHtml(answer.option_Value)+'>Y</td>';
            						}else if(answer.answer=="0"){
            							answers += '<td class="col-td-question-answer" title='+switcHtml(answer.option_Value)+'>N</td>';
            						}else{
            							answers += '<td class="col-td-question-answer" title='+switcHtml(answer.option_Value)+'>'+switcHtml(answer.answer)+'</td>';
            						}
            					}
            				});
            				
            				if(!flag)
            				{
            					answers += '<td class="col-td-question-answer">N/A</td>';
            				}
        				}
        				else
        				{
        					answers += '<td class="col-td-question-answer">N/A</td>';
        				}
            		});
        			//拼接投票结果列表
        			tableContent += '<tr><td  class="col-username">'+switcHtml(result.username)+'</td><td  class="col-username">'+switcHtml(result.cn_name)+'</td><td  class="col-username">'+switcHtml(result.dept_name)+'</td><td  class="col-vote-date">'+time+'</td>'+answers+'<td style="width: 1px;"></td></tr>';
        			
        			//tableContent += '<tr><td  class="col-username">'+switcHtml(result.username)+'</td><td  class="col-vote-date">'+time+'</td>'+answers+'<td style="width: 1px;"></td></tr>';

        			totalResult=data.queryParam.totalResult;
        		});
        	}
        	$("#QualityResultList").html(tableContent);
        	
        	//分页处理
        	currentPage = pageIndex;
    		if(totalResult == 0)
    		{
    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
    			//隐藏分页容器
    			$("#pagination").hide();
    		} 
    		else 
    		{
    			//显示分页容器
    			$("#pagination").show();
    			// 创建分页
    			$("#pagination").pagination(totalResult, 
    			{
    				num_edge_entries: 1, //边缘页数
    				num_display_entries: 3, //主体页数
    				current_page:currentPage,
    				callback: getDataList,
    				items_per_page:showCount //每页显示1项
    			});
    		};
        },
        error: function()
        {
        	openAlert("加载数据错误！");
        	return;
        }
	});
};

/**
 * 转换不规则的空数据
 * @param str 字符串
 */
function getRealNull(str)
{
	if(str===null || str==="" || str==="null")
	{
		return "";
	}
	return str;
};
